﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MyBlog2.Repository;
using MyBlog2.Models;
using Microsoft.AspNetCore.Authorization;

namespace MyBlog2.Areas.Admin.Controllers
{
    [Area("Admin")]
    [Authorize]
    public class TagManageController : Controller
    {
        private readonly ITagRepository _tagRepository;

        public TagManageController(ITagRepository tagRepository)
        {
            this._tagRepository = tagRepository;
        }

        [Route("Admin/TagManage/TagManagePage", Name = "TagManagePage")]
        public IActionResult TagManagePage()
        {
            var t = _tagRepository.GetTagList();
            return View(t);
        }

        [HttpGet]
        [Route("Admin/TagManage/EditTag", Name = "EditTag")]
        public IActionResult EditTag(int id)
        {
            var tag = _tagRepository.GetOneTag(id);
            return View(tag);
        }

        /// <summary>
        /// 编辑标签提交
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="nouse"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Admin/TagManage/EditTag", Name = "EditTagPost")]
        public IActionResult EditTag(Tag tag,string nouse)
        {
            if (ModelState.IsValid)
            {
                var t = _tagRepository.EditTag(tag);
                if (t == null)
                {
                    return BadRequest("编辑标签失败！");
                }
                return Redirect("/Admin/TagManage/TagManagePage");
            }
            else
            {
                return BadRequest("填写数据有误！");
            }
        }


        /// <summary>
        /// 删除标签
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Admin/TagManage/DeleteTag", Name = "DeleteTag")]
        public IActionResult DeleteTag(int id)
        {
            if (_tagRepository.DeleteTag(id))   //删除成功
            {
                return Redirect("/Admin/TagManage/TagManagePage");
            }
            else
            {
                return BadRequest("删除失败！");
            }
        }


        [Route("Admin/TagManage/AddTag", Name = "AddTag")]
        public IActionResult AddTag(Tag tag)
        {
            return View(tag);
        }

        /// <summary>
        /// 添加标签提交
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="nouse"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Admin/TagManage/AddTag", Name = "AddTagPost")]
        public IActionResult AddTagPost([Bind("Name")] Tag tag)
        {
            if (ModelState.IsValid)
            {
                var isTag = _tagRepository.GetOneTag(tag.Name);
                if (isTag != null)   //标签已存在
                {
                    return BadRequest("标签已存在!");
                }
                var t = _tagRepository.AddTag(tag);
                if (t == null)
                {
                    return BadRequest("标签添加失败！");
                }
                return Redirect("/Admin/TagManage/TagManagePage");
            }
            else
            {
                return BadRequest("填写数据有误！");
            }

        }
    }
}